{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Cheat Sheet\n",
    "\n",
    "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n",
    "\n",
    "## Read It\n",
    "\n",
    "- [Website](https://www.pythoncheatsheet.org)\n",
    "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n",
    "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n",
    "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n",
    "\n",
    "## itertools Module\n",
    "\n",
    "The _itertools_ module is a collection of tools intented to be fast and use memory efficiently when handling iterators (like [lists](#lists) or [dictionaries](#dictionaries-and-structuring-data)).\n",
    "\n",
    "From the official [Python 3.x documentation](https://docs.python.org/3/library/itertools.html):\n",
    "\n",
    "> The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.\n",
    "\n",
    "The _itertools_ module comes in the standard library and must be imported.\n",
    "\n",
    "The [operator](https://docs.python.org/3/library/operator.html) module will also be used. This module is not necessary when using itertools, but needed for some of the examples below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "import operator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### accumulate\n",
    "\n",
    "Makes an iterator that returns the results of a function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.accumulate(iterable[, func])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [1, 2, 3, 4, 5]\n",
    "result = itertools.accumulate(data, operator.mul)\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The operator.mul takes two numbers and multiplies them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "operator.mul(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "operator.mul(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "operator.mul(6, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "operator.mul(24, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Passing a function is optional:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [5, 2, 6, 4, 5, 9, 1]\n",
    "result = itertools.accumulate(data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If no function is designated the items will be summed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5\n",
    "5 + 2 = 7\n",
    "7 + 6 = 13\n",
    "13 + 4 = 17\n",
    "17 + 5 = 22\n",
    "22 + 9 = 31\n",
    "31 + 1 = 32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### combinations\n",
    "\n",
    "Takes an iterable and a integer. This will create all the unique combination that have r members."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.combinations(iterable, r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shapes = ['circle', 'triangle', 'square',]\n",
    "result = itertools.combinations(shapes, 2)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### combinations_with_replacement\n",
    "\n",
    "Just like combinations(), but allows individual elements to be repeated more than once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.combinations_with_replacement(iterable, r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shapes = ['circle', 'triangle', 'square']\n",
    "result = itertools.combinations_with_replacement(shapes, 2)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### count\n",
    "\n",
    "Makes an iterator that returns evenly spaced values starting with number start."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.count(start=0, step=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in itertools.count(10,3):\n",
    "   print(i)\n",
    "   if i > 20:\n",
    "       break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cycle\n",
    "\n",
    "This function cycles through an iterator endlessly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.cycle(iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']\n",
    "for color in itertools.cycle(colors):\n",
    "   print(color)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When reached the end of the iterable it start over again from the beginning.\n",
    "\n",
    "### chain\n",
    "\n",
    "Take a series of iterables and return them as one long iterable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.chain(*iterables)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue']\n",
    "shapes = ['circle', 'triangle', 'square', 'pentagon']\n",
    "result = itertools.chain(colors, shapes)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### compress\n",
    "\n",
    "Filters one iterable with another."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.compress(data, selectors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shapes = ['circle', 'triangle', 'square', 'pentagon']\n",
    "selections = [True, False, True, False]\n",
    "result = itertools.compress(shapes, selections)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### dropwhile\n",
    "\n",
    "Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.dropwhile(predicate, iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]\n",
    "result = itertools.dropwhile(lambda x: x<5, data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### filterfalse\n",
    "\n",
    "Makes an iterator that filters elements from iterable returning only those for which the predicate is False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.filterfalse(predicate, iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "result = itertools.filterfalse(lambda x: x<5, data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### groupby\n",
    "\n",
    "Simply put, this function groups things together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.groupby(iterable, key=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "robots = [{\n",
    "    'name': 'blaster',\n",
    "    'faction': 'autobot'\n",
    "}, {\n",
    "    'name': 'galvatron',\n",
    "    'faction': 'decepticon'\n",
    "}, {\n",
    "    'name': 'jazz',\n",
    "    'faction': 'autobot'\n",
    "}, {\n",
    "    'name': 'metroplex',\n",
    "    'faction': 'autobot'\n",
    "}, {\n",
    "    'name': 'megatron',\n",
    "    'faction': 'decepticon'\n",
    "}, {\n",
    "    'name': 'starcream',\n",
    "    'faction': 'decepticon'\n",
    "}]\n",
    "\n",
    "for key, group in itertools.groupby(robots, key=lambda x: x['faction']):\n",
    "   print(key)\n",
    "   print(list(group))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### islice\n",
    "\n",
    "This function is very much like slices. This allows you to cut out a piece of an iterable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.islice(iterable, start, stop[, step])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue',]\n",
    "few_colors = itertools.islice(colors, 2)\n",
    "\n",
    "for each in few_colors:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### permutations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.permutations(iterable, r=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha_data = ['a', 'b', 'c']\n",
    "result = itertools.permutations(alpha_data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### product\n",
    "\n",
    "Creates the cartesian products from a series of iterables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_data = [1, 2, 3]\n",
    "alpha_data = ['a', 'b', 'c']\n",
    "result = itertools.product(num_data, alpha_data)\n",
    "\n",
    "for each in result:\n",
    "    print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### repeat\n",
    "\n",
    "This function will repeat an object over and over again. Unless, there is a times argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.repeat(object[, times])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in itertools.repeat(\"spam\", 3):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### starmap\n",
    "\n",
    "Makes an iterator that computes the function using arguments obtained from the iterable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.starmap(function, iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [(2, 6), (8, 4), (7, 3)]\n",
    "result = itertools.starmap(operator.mul, data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### takewhile\n",
    "\n",
    "The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.takewhile(predicate, iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]\n",
    "result = itertools.takewhile(lambda x: x<5, data)\n",
    "\n",
    "for each in result:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### tee\n",
    "\n",
    "Return n independent iterators from a single iterable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.tee(iterable, n=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue']\n",
    "alpha_colors, beta_colors = itertools.tee(colors)\n",
    "\n",
    "for each in alpha_colors:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue']\n",
    "alpha_colors, beta_colors = itertools.tee(colors)\n",
    "\n",
    "for each in beta_colors:\n",
    "   print(each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### zip_longest\n",
    "\n",
    "Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itertools.zip_longest(*iterables, fillvalue=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['red', 'orange', 'yellow', 'green', 'blue',]\n",
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]\n",
    "\n",
    "for each in itertools.zip_longest(colors, data, fillvalue=None):\n",
    "   print(each)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
